$(function() {
    //Listeners
    $('#price_select_city').change(function() {
        changeHotelByCity($(this).val());
    });

    $('#price_select_hotel').change(function() {
        changeRoomsByHotel($(this).val());
    });

    $('#price_button_search').on('click', function() {
        searchPrice();
    });

    cheakSearchInformation(searchParamPrice);

    $('#price_button_update').on('click', function() {
        updatePrices();
    });

    $('.ui_gray_short_button').on('click', function() {
        clearThisPrice(this);
    });

    $('.ui_orange_short_button').on('click', function() {
        updateThisPrice(this);
    });
});

/**
 * clear current price in db
 * @param {type} element
 * @returns {undefined}
 */
function clearThisPrice(element) {
    var parent = $(element).parent().parent();

    var update = $(parent).find('input.ui_gray_short_button').parent();

    var nprice = {
        id: $(parent).attr('price_id'),
        fit_usd: 0,
        fit_nis: 0,
        special_usd: 0,
        special_nis: 0,
        cascade_usd: 0,
        cascade_nis: 0,
        market_usd: 0
    };

    $.ajax({
        type: 'POST',
        url: urlUpdatePrice,
        data: nprice,
        beforeSend: function() {
            $(update).find('input.ui_gray_short_button').hide();
            $(update).find('div.ui_short_button_loader').show();
        },
        success: function(msg) {
            window.location.reload();
        }
    });
}

/**
 * update current price in db
 * @param {type} element
 * @returns {undefined}
 */
function updateThisPrice(element) {
    var parent = $(element).parent().parent();

    var update = $(parent).find('input.ui_orange_short_button').parent();

    var nprice = {
        id: $(parent).attr('price_id'),
        fit_usd: $(parent).find('input.fit_usd').val(),
        fit_nis: $(parent).find('input.fit_nis').val(),
        special_usd: $(parent).find('input.special_usd').val(),
        special_nis: $(parent).find('input.special_nis').val(),
        cascade_usd: $(parent).find('input.cascade_usd').val(),
        cascade_nis: $(parent).find('input.cascade_nis').val(),
        market_usd: $(parent).find('input.market_usd').val()
    };

    $.ajax({
        type: 'POST',
        url: urlUpdatePrice,
        data: nprice,
        beforeSend: function() {
            $(update).find('input.ui_orange_short_button').hide();
            $(update).find('div.ui_short_button_loader').show();
        },
        success: function(msg) {
            window.location.reload();
        }
    });
}

/**
 * update or create data base fields with prices
 * after success response reload this page
 */
function updatePrices() {
    if (parseInt(searchParamPrice.hotel) === 0) {
        return false;
    }

    var nprice = {
        hotel: searchParamPrice.hotel,
        room: searchParamPrice.room,
        from: searchParamPrice.from,
        to: searchParamPrice.to,
        period: $('#price_select_period').val(),
        days: $('input[type="radio"]:checked').val(),
        fit_usd: $('#price_fit_usd').val(),
        fit_nis: $('#price_fit_nis').val(),
        special_usd: $('#price_fit_special_usd').val(),
        special_nis: $('#price_fit_special_nis').val(),
        cascade_usd: $('#price_cascade_usd').val(),
        cascade_nis: $('#price_cascade_nis').val(),
        market_usd: $('#price_russian_market_usd').val()
    };

    $.ajax({
        type: 'POST',
        url: urlUpdatePrice,
        data: nprice,
        beforeSend: function() {
            $('#price_button_update').hide();
            $('#price_button_update_loader').show();
        },
        success: function(msg) {
            window.location.reload();
        }
    });
}

/**
 * insert current information to fields
 * @param {type} info 
 * @returns {undefined}
 */
function cheakSearchInformation(info) {
    if (info.city !== '' && parseInt(info.city) !== 0) {
        changeHotelByCity(info.city);
        changeRoomsByHotel(info.hotel);

        $('.ui_title_string_block').html(info.string);
        $('.ui_title_string_block').css({padding: '2px'});
    }
}

/**
 * get and check parameters for search
 * city_id, hotel id, room id, from date, to date
 * and refresh this page with GET parameters
 */
function searchPrice() {
    var city = $('#price_select_city').val();
    var hotel = $('#price_select_hotel').val();
    var room = $('#price_select_room').val();
    var from = $('#price_select_from_date').val();
    var to = $('#price_select_to_date').val();

    if (parseInt(city) === 0) {
        $('.ui_line_error').html('** please select city.');
        return;
    }

    if (parseInt(hotel) === 0) {
        $('.ui_line_error').html('** please select a hotel.');
        return;
    }

    if (parseInt(room) === 0) {
        $('.ui_line_error').html('** please select a room.');
        return;
    }

    if (from === '') {
        $('.ui_line_error').html('** please select from date.');
        return;
    }

    if (to === '') {
        $('.ui_line_error').html('** please select to date.');
        return;
    }

    window.location.assign(urlBackend + 'searchprice/city/' + city + '/hotel/' + hotel + '/room/' + room + '/from/' + from + '/to/' + to);
}

/**
 * insert hotels to select field after choice city
 * @param {type} city
 * @returns {undefined}
 */
function changeHotelByCity(city) {
    $('#price_select_hotel option').remove();
    var select = $('#price_select_hotel');

    (select).append($('<option value="0" selected="selected">Please select a hotel</option>'));
    $(hotelsPrice).each(function(i, v) {
        if (parseInt(v.city_id) === parseInt(city)) {
            if (parseInt(v.id) === parseInt(searchParamPrice.hotel)) {
                var option = $('<option />').attr({'value': v.id, 'selected': 'selected'}).html(v.name);
            } else {
                var option = $('<option />').attr({'value': v.id}).html(v.name);
            }
            (select).append(option);
        }
    });
}

/**
 * insert rooms to select field after choice hotel
 * @param {type} hotel
 * @returns {undefined}
 */
function changeRoomsByHotel(hotel) {
    $('#price_select_room option').remove();
    var select = $('#price_select_room');

    (select).append($('<option value="0" selected="selected">Please select a room</option>'));
    $(roomsPrice).each(function(i, v) {
        if (parseInt(v.hotel_id) === parseInt(hotel)) {
            if (parseInt(v.id) === parseInt(searchParamPrice.room)) {
                var option = $('<option />').attr({'value': v.id, 'selected': 'selected'}).html(v.name);
            } else {
                var option = $('<option />').attr({'value': v.id}).html(v.name);
            }
            (select).append(option);
        }
    });
}


